package com.dinadi.yidi.dialog;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.dinadi.yidi.R;


/**
 * Created by mimizaijia on 2016/12/1.
 */

public class MyProgressDialog extends ProgressDialog {
    private AnimationDrawable mAnimation;
    private ImageView mImageView;
    private TextView mTextView;
    private String loadingTip;
    private Context context;
    private int resid;

    /**
     * @param context 上下文对象
     * @param content 显示文字提示信息内容
     * @param
     */
    public MyProgressDialog(Context context, int theme, String content, int resid) {
        super(context, theme);
        this.context = context;
        this.loadingTip = content;
        this.resid = resid;
        //点击提示框外面是否取消提示框
        setCanceledOnTouchOutside(false);
        //点击返回键是否取消提示框
        setCancelable(false);
        setIndeterminate(true);
    }

    public void setCanCancel() {
        //点击提示框外面是否取消提示框
        setCanceledOnTouchOutside(true);
        //点击返回键是否取消提示框
        setCancelable(true);

    }

    public MyProgressDialog(Context context, int theme) {
        this(context, theme, "加载中", R.drawable.loading);
    }

    public void openProgressDialog(String message) {
        // 通过ImageView对象拿到背景显示的AnimationDrawable
        show();
        mTextView.setText(message);
        mAnimation = (AnimationDrawable) mImageView.getBackground();
        mImageView.post(new Runnable() {
            @Override
            public void run() {
                mAnimation.start();
            }
        });
    }

    public void closeProgressDialog() {
        mAnimation = (AnimationDrawable) mImageView.getBackground();
        mImageView.post(new Runnable() {
            @Override
            public void run() {
                mAnimation.stop();
            }
        });
        if (this.isShowing()) {
            dismiss();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.progress_dialog);
        //设置dialog的位置
        Window dialogWindow = this.getWindow();
        dialogWindow.setBackgroundDrawableResource(android.R.color.transparent);
        dialogWindow.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
        mTextView = (TextView) findViewById(R.id.loadingTv);
        mImageView = (ImageView) findViewById(R.id.loadingIv);
//        Glide.with(context).load(R.drawable.aaa)
//                .diskCacheStrategy(DiskCacheStrategy.ALL).into(mImageView);
        mImageView.setBackgroundResource(resid);
    }

}
